<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="700px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="150px">
          <el-form-item label="帮忙编码" prop="helpNo">
            <el-input v-model="form.helpNo" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="类型" prop="helpType">
            <el-select v-model="form.helpType" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.TANZER_HELP_TYPE"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="标题" prop="helpTitle">
            <el-input v-model="form.helpTitle" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="详情">
            <el-input v-model="form.helpDetail" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="图片">
              <MaterialList
                      v-model="helpDetailPicArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
          </el-form-item>
          <el-form-item label="开始时间">
            <el-date-picker v-model="form.projectStartDate" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="结束时间">
            <el-date-picker v-model="form.projectEndDate" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注说明">
            <el-input v-model="form.remark" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="起始地址" prop="firstAddrDetail">
            <el-input v-model="form.firstAddrDetail" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="到达地址" prop="endAddrDetail">
            <el-input v-model="form.endAddrDetail" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="帮助金额">
            <el-input v-model="form.helpPrice" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="奖金">
            <el-input v-model="form.reward" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input v-model="form.concatPhone" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="发布人" prop="releaseUserId">
            <el-input
              v-if="false"
              v-model="form.releaseUserId"
              style="width: 370px;"
            />
            <div v-if="form.releaseUserId">
              {{ dialogTableDataReleaseUserId.showInputValue }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataReleaseUserId"
              @select-callback="dialogTableSelectedCallBackReleaseUserId"
            ></dialogTable>
          </el-form-item>
          <el-form-item label="接单人" prop="takeTeamId">
            <el-input
              v-if="false"
              v-model="form.takeTeamId"
              style="width: 370px;"
            />
            <div v-if="form.takeTeamId">
              {{ dialogTableDatatTakeTeamId.showInputValue }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDatatTakeTeamId"
              @select-callback="dialogTableSelectedCallBackTakeTeamId"
            ></dialogTable>
          </el-form-item>
          <el-form-item label="订单接收时间">
            <el-date-picker v-model="form.orderReceiptTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="订单开始时间">
            <el-date-picker v-model="form.orderStartTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="订单结束时间">
            <el-date-picker v-model="form.orderEndTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="帮忙状态">
            <el-select v-model="form.status" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.HELP_STATUS"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('helpNo')" prop="helpNo'" label="帮忙编码" />
        <el-table-column v-if="columns.visible('helpType')" prop="helpType" label="类型">
          <template slot-scope="scope">
            {{ dict.label.TANZER_HELP_TYPE[scope.row.helpType] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('helpTitle')" prop="helpTitle" label="标题" />
          <el-table-column v-if="columns.visible('helpDetailPic')" prop="helpDetailPic" label="图片">
            <template slot-scope="scope">
              <a :href="scope.row.helpDetailPic" style="color: #42b983" target="_blank">
                <img :src="scope.row.helpDetailPic" alt="点击打开" class="table-img" />
              </a>
            </template>
          </el-table-column>
        <el-table-column v-if="columns.visible('projectStartDate')" prop="projectStartDate" label="项目开始时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.projectStartDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('projectEndDate')" prop="projectEndDate" label="项目结束时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.projectEndDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('remark')" prop="remark" label="备注说明" />
        <el-table-column v-if="columns.visible('helpPrice')" prop="helpPrice" label="帮助金额" />
        <el-table-column v-if="columns.visible('reward')" prop="reward" label="奖金" />
        <el-table-column v-if="columns.visible('releaseUserName')" prop="releaseUserName" label="发布人" />
        <el-table-column v-if="columns.visible('takeTeamName')" prop="takeTeamName" label="接收人" />
        <el-table-column v-if="columns.visible('status')" prop="status" label="帮忙状态">
          <template slot-scope="scope">
            {{ dict.label.HELP_STATUS[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','tanzerHelp:edit','tanzerHelp:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTanzerHelp from '@/api/tanzer/tanzerHelp'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";
import dialogTable from "@/views/components/dialogTable";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '帮忙表', url: 'api/tanzerHelp', sort: 'id,desc', crudMethod: { ...crudTanzerHelp }})
const defaultForm = { id: null, helpTitle: null, helpType: null, helpDetail: null, helpDetailPic: null, startDate: null, endDate: null, remark: null, firstAddr: null, endAddr: null, helpPrice: null, releaseUserId: null, createTime: null, createBy: null, updateBy: null, updateTime: null, isDel: null, helpStatus: null }
export default {
  name: 'TanzerHelp',
  components: { dialogTable, pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['TANZER_HELP_TYPE', 'is_enable', 'HELP_STATUS'],
  data() {
    return {
      helpDetailPicArr:[],
      permission: {
        add: ['admin', 'tanzerHelp:add'],
        edit: ['admin', 'tanzerHelp:edit'],
        del: ['admin', 'tanzerHelp:del']
      },
      rules: {
        helpTitle: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        helpType: [
          { required: true, message: '类型不能为空', trigger: 'blur' }
        ],
        firstAddr: [
          { required: true, message: '取yx_user_address的id不能为空', trigger: 'blur' }
        ],
        endAddr: [
          { required: true, message: '取yx_user_address的id不能为空', trigger: 'blur' }
        ],
        beYxUserId: [
          { required: true, message: '发布人不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'helpNo', display_name: '帮忙编码' },
        { key: 'helpTitle', display_name: '标题' },
      ],

      // 用户数据源
      dialogTableDataReleaseUserId: {
        title: "用户信息",
        dialogVisible: false,
        url: "api/yxUser",
        sort: "uid,desc",
        buttonContext: "选择用户",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'username', display_name: '用户账户' },
          { key: 'realName', display_name: '用户姓名' },
        ],
        elTtableColumns: [
          {
            columnProp: "username",
            columnLabel: "用户账户"
          },
          {
            columnProp: "realName",
            columnLabel: "用户姓名"
          },
          {
            columnProp: "level",
            columnLabel: "等级"
          },
          {
            columnProp: "userType",
            columnLabel: "用户类型"
          }
        ]
      },

      // 团队数据源
      dialogTableDatatTakeTeamId: {
        title: "团队成员",
        dialogVisible: false,
        url: "api/tanzerBaseTeam",
        sort: "id,desc",
        buttonContext: "选择团队成员",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'name', display_name: '名称' },
          { key: 'studentPhone', display_name: '电话' },
          { key: 'status', display_name: '状态' }
        ],
        elTtableColumns: [
          {
            columnProp: "name",
            columnLabel: "名称"
          },
          {
            columnProp: "studentPhone",
            columnLabel: "电话"
          },
          {
            columnProp: "status",
            columnLabel: "状态"
          }
        ]
      },
    }
  },
  watch: {
    "helpDetailPicArr":function(val) {
      if (val) {
        this.form.helpDetailPic = val.join(",");
      }
    },
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.helpTitle
        delete this.crud.params.helpType
        delete this.crud.params.beYxUserId
        delete this.crud.params.isDel
        delete this.crud.params.helpStatus
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.helpDetailPic && form.id) {
        this.helpDetailPicArr = form.helpDetailPic.split(",");
      }else{
        this.helpDetailPicArr=[]
      }
    },

    //用户选择
    dialogTableSelectedCallBackReleaseUserId(row) {
      this.form.releaseUserId = row.uid;
      if (this.form.releaseUserId) {
        this.dialogTableDataReleaseUserId.showInputValue = row.realName;
      }
    },

    //团队成员选择
    dialogTableSelectedCallBackTakeTeamId(row) {
      this.form.takeTeamId = row.id;
      if (this.form.takeTeamId) {
        this.dialogTableDatatTakeTeamId.showInputValue = row.name;
      }
    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
